/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.tools;

import java.util.Set;

import dgbgui.command.AddQueryCommand;
import dgbgui.command.CommandStack;
import dgbgui.command.GroupCommand;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.definition.FilteredConceptDefinition;
import dgbgui.modeldocument.definition.FilteredLayerDefinition;
import dgbgui.modeldocument.definition.FilteredRelationDefinition;
import dgbgui.modeldocument.primitives.ConceptType;
import dgbgui.modeldocument.primitives.ConceptronType;
import dgbgui.modeldocument.primitives.LayerType;
import dgbgui.modeldocument.primitives.RelationType;
import dgbgui.selection.SelectionManager;

public class CreateSetTool implements Tool {

    public void execute() {

        // Get the selected type nodes. Only conceptron type nodes may be
        // selected for this tool
        Set<Object> selection = SelectionManager
                .getInstance().getSelection(ConceptronType.class);
        assert (selection.size() > 0);

        GroupCommand groupCommand = new GroupCommand();

        // Create prototype nodes for each type node
        for (Object obj : selection) {
            assert ( obj instanceof ConceptronType );
            Query prototypeNode = null;
            if ( obj instanceof ConceptType ) {
                prototypeNode= new Query(
                        new FilteredConceptDefinition((ConceptType)obj));
            } else if ( obj instanceof RelationType ) {
                prototypeNode = new Query(
                        new FilteredRelationDefinition((RelationType)obj));
            } else if ( obj instanceof LayerType ) {
                prototypeNode = new Query(
                        new FilteredLayerDefinition((LayerType)obj));
            }    
            assert(prototypeNode != null);
            groupCommand.add(new AddQueryCommand(prototypeNode));
        }
        
        if (groupCommand.size() > 0) {
            CommandStack.getInstance().execute(groupCommand);
        }

    }

}
